package com.xonro.common.histrans.sfeg.biz;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.common.SfegRecordTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CustomerBiz extends SfegRecordTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_MDM_CUSTOMER");//往来单位信息
        logModel.setOuterTable("tb_khxx");//合格供方信息表
        logModel.setOuterPK(mainData.getString("khbh"));
        logModel.setOuterExt1(mainData.getString("khmc"));
        logModel.setOuterExt2(mainData.getString("kelx"));//客户类型
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("COMPANY_NAME",mainData.getString("khmc"));//公司名称
        mainBO.set("COMPANY_NO",mainData.getString("khbh"));//公司编号
        mainBO.set("LEGAL_MAN",mainData.getString("frdb"));//法人代表
        mainBO.set("LINKMAN",mainData.getString("lxr"));//联系人
        mainBO.set("MOBILE",mainData.getString("lxdh"));//联系人电话
        mainBO.set("COMPANY_CATEGORY",mainData.getString("kelx"));//往来单位类别，客户类型
        mainBO.set("FULL_ADDRESS",mainData.getString("dwdz"));//单位地址
        mainBO.set("POSTAL_CODE",mainData.getString("yb"));//邮编
        mainBO.set("MAILBOX",mainData.getString("dzxx"));//电子邮件
        mainBO.set("MAILBOX",mainData.getString("dzxx"));//使用范围
        mainBO.set("SCOPE",mainData.getString("JYFW"));//经营范围
        mainBO.set("KEY_PERFORMANCE",mainData.getString("ZYYJ"));//主要业绩
        mainBO.set("SUBPACKAGE_SPLIT",mainData.getString("ISFBCF"));//是否拆分包
        mainBO.set("COMPANY_TYPE",mainData.getString("y_gslx"));//公司类型
        if(StringUtils.isNotBlank(mainData.getString("y_clrq"))&&mainData.getString("y_clrq").length()==10) {

            mainBO.set("COMPANY_TYPE", DateUtil.parse(mainData.getString("y_clrq").replace("/","-"), "yyyy-MM-dd"));//成立日期
        }
        if(StringUtils.isNotBlank(mainData.getString("y_yyqxs"))&&mainData.getString("y_yyqxs").length()==10) {
            mainBO.set("START_DATE", DateUtil.parse(mainData.getString("y_yyqxs").replace("/","-"), "yyyy-MM-dd"));//营业开始日期
        }
        if(StringUtils.isNotBlank(mainData.getString("y_yyqxe"))&&mainData.getString("y_yyqxe").length()==10) {
            mainBO.set("END_DATE", DateUtil.parse(mainData.getString("y_yyqxe").replace("/","-"), "yyyy-MM-dd"));//营业结束日期
        }
        mainBO.set("NO",mainData.getString("s_bh"));//编号
        mainBO.set("REGISTERED_FUND_STRING",mainData.getString("y_zczb"));//注册资本
     //   mainBO.set("NO",mainData.getString("y_sszb"));//实收资本
        mainBO.set("ENGINEERING_SITE",mainData.getString("z_dz"));//详细地址
        mainBO.set("RECIPIENT_BANK_NAME",mainData.getString("BankName"));// 收款单位开户银行
        mainBO.set("RECIPIENT_BANK_USER",mainData.getString("BankAccount"));// 客户银行账号
        //往来单位-合格供方子表 BO_XR_MDM_CUSTOMER_LIST
        List<RowMap> sonDatas= SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_khxxFS_detail detail LEFT JOIN tb_khxxFS fs ON detail.fsid= fs.fsid where detail.khbh= ? AND yx='1' ",mainData.getString("khbh"));
        for(RowMap son:sonDatas){
            String uuid = UUIDGener.getUUID();
            BOCreateData subBOCreateData = new BOCreateData();
            boCreateDataList.add(subBOCreateData);
            subBOCreateData.setCreateUser(createUser);
            subBOCreateData.setBoName("BO_XR_MDM_CUSTOMER_LIST");
            BO subBO = new BO();
            subBO.setId(uuid);
            subBOCreateData.setBo(subBO);
            subBO.set("REMARK",son.getString("fsbz"));//备注
            subBO.set("SUPPLY_STATUS",son.getString("status"));//状态
            subBO.set("SUPPLY_STATUS",son.getString("yjpd"));//业绩评定结果：1优良；2合格；3不合
            mainBO.set("SUPPLY_TYPE",mainData.getString("kelx"));//往来单位类别，供方类型
            super.addLogModel(historyTransContext, "tb_khxxFS", son.getString("fsid"), "BO_XR_MDM_CUSTOMER_LIST", uuid, bindId);
        /*    FileTransData fileData = new FileTransData();
            fileData.setBoId(uuid);
            //印章id
            fileData.setFkCode(mainData.getString("suCode"));
            fileData.setProcessInstId(bindId);
            historyTransContext.getFileTransDatas().add(fileData);
            fileData.setBoName("BO_XR_SEAL_APPLY_LIST");
            fileData.setFieldName("ATTACHMENT");//*/
        }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);


        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("khbh"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_MDM_CUSTOMER");
        fileTransData.setBoName("BO_XR_MDM_CUSTOMER");
        fileTransData.setFieldName("ATTACHMENT");
    }

    @Override
    protected String getProcessDefId() {
        return "obj_255a75764d83482aa8f791b2cd2792dd";
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }

    @Override
    protected String getActivityDefId() {
        return null;
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        String customerNo=mainData.getString("khbh");
        String customerName=mainData.getString("khmc");
        return customerNo+customerName;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("khbh");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_khxx";
    }


    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("khbh");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        //查询实施单位是康益海洋的公司
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_khxx  ");
    }
}
